﻿

local moduleName = "HealthBar"

local L = {
	[moduleName] = "Health Bar",

	["Agro Color"] = true,
	["Color the healthbar of the person with agro red."] = true,

	["Texture"] = true,
	["The texture used for the health bar."] = true,

	["Deficit"] = true,
	["Show how much health a person is missing instead of how much he has."] = true,

	["Class Color"] = true,
	["Color the healthbars according class."] = true,

	["Width"] = true,
	["Width of the healthbar."] = true
}

if GetLocale() == "zhCN" then
	L[moduleName] = "生命条"

	L["Agro Color"] = "仇恨颜色"
	L["Color the healthbar of the person with agro red."] = "产生仇恨后改变生命条的颜色"

	L["Texture"] = "贴图"
	L["The texture used for the health bar."] = "生命条的贴图"

	L["Deficit"] = "治疗模式"
	L["Show how much health a person is missing instead of how much he has."] = "显示该角色损失的生命值,治疗职业使用"

	L["Class Color"] = "职业颜色"
	L["Color the healthbars according class."] = "生命条根据不同职业显示不同颜色"

	L["Width"] = "宽"
	L["Width of the healthbar."] = "生命条的宽度"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "生命條"

	L["Agro Color"] = "仇恨顏色"
	L["Color the healthbar of the person with agro red."] = "產生仇恨後改變生命條的顏色"

	L["Texture"] = "貼圖"
	L["The texture used for the health bar."] = "生命條的貼圖"
	L["Deficit"] = "治療模式"

	L["Show how much health a person is missing instead of how much he has."] = "顯示該角色損失的生命值,治療職業使用"

	L["Class Color"] = "職業顏色"
	L["Color the healthbars according class."] = "生命條根據不同職業顯示不同顏色"

	L["Width"] = "寬"
	L["Width of the healthbar."] = "生命條的寬度"
end
----------------------------------
--      Local Declaration      --
----------------------------------

local SharedMedia     = LibStub("LibSharedMedia-3.0")
local SharedMediaType = SharedMedia.MediaType and SharedMedia.MediaType.STATUSBAR or "statusbar"
local tbl = SharedMedia:List(SharedMediaType)
local banzai = LibStub("LibBanzai-2.0")

local agro = { }

local UnitHealthMax = UnitHealthMax
local UnitHealth = UnitHealth
local UnitClass = UnitClass
local RAID_CLASS_COLORS = RAID_CLASS_COLORS

----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName)

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		agrocolor = {
			type     = "toggle",
			name     = L["Agro Color"],
			desc     = L["Color the healthbar of the person with agro red."],
			disabled = function() return not BunchOfBars.modules["Agro"] end,
            		get      = "GetSetAgroColor",
            		set      = "GetSetAgroColor"
		},
--[[		texture = {
			type = 'select',
			name = L["Texture"],
			desc = L["The texture used for the health bar."],
			get  = "GetSetTexture",
			set  = "GetSetTexture",
			values = tbl,
		},]]
		deficit = {
			type     = "toggle",
			name     = L["Deficit"],
			desc     = L["Show how much health a person is missing instead of how much he has."],
            		get      = "GetSetDeficit",
            		set      = "GetSetDeficit"
		},
		classcolor = {
			type     = "toggle",
			name     = L["Class Color"],
			desc     = L["Color the healthbars according class."],
            		get      = "GetSetClassColor",
            		set      = "GetSetClassColor"
		}
	}
}

plugin.defaultDB = {
	profile = {
		agrocolor  = true,
		texture    = "Interface\\AddOns\\BunchOfBars\\Textures\\bar13",
		deficit    = false,
		classcolor = false,
		vertical = false,
		ltr = false,
		style = {
			position = "BOTTOMLEFT",
			strata = 1,
			inout = 2,
			xof = 2,
			yof = 2,
			width = 0,
			height = 12,
		}
	}
}
plugin.head = {
	core = true,
}
plugin.moduletype = "statusbar"
----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
--	self:RegisterBucketEvent({"UNIT_HEALTH", "UNIT_MAXHEALTH", "BunchOfBars_Agro"}, 0.05, "UpdateUnits")
	self:RegisterEvent("UNIT_HEALTH", "UpdateUnit")
	self:RegisterEvent("UNIT_MAXHEALTH", "UpdateUnit")
--	self:RegisterMessage("BunchOfBars_Agro", "UpdateUnit")
	banzai:RegisterCallback(callback)

end

function callback(aggro, unit, unitid)
	BunchOfBars:GetModule(moduleName):UpdateUnit(_, unitid)
end

function plugin:OnCreate(frame)
--	local parent = frame:GetParent()
--	if not self.head[parent.head] then return end
--	local texture = (SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
	local bar = CreateFrame("Frame", nil, frame)
	bar:Show()

	local back = bar:CreateTexture(nil, "BACKGROUND")
	back:SetTexture(self.db.profile.texture)
	back:SetTexCoord(unpack(self:SetTextureCoord(self.db.profile.vertical)))
	back:SetAlpha(0.5)
	back:ClearAllPoints()
	back:SetAllPoints(bar)
	back:Show()
	bar.back = back

	local front = bar:CreateTexture(nil, "ARTWORK")
	front:SetTexture(self.db.profile.texture)
	front:SetTexCoord(unpack(self:SetTextureCoord(self.db.profile.vertical)))
	front:Show()
	bar.front = front
	self:SetBarPoint(bar, self.db.profile.ltr, self.db.profile.vertical)

	return bar
end

function plugin:OnUpdate(frame, unitid)
--	local parent = frame:GetParent()
--	if not self.head[parent.head] then return end
	if not unitid then
		unitid = frame.unit
	end
	if not UnitExists(unitid) then return end
	local bar = frame.parts[moduleName]
	local m = UnitHealthMax(unitid)
	local c = UnitHealth(unitid)
	local p = c / m
	local size
	local color = {}
	local threat = UnitThreatSituation(unitid, "target")

	if banzai:GetUnitAggroByUnitId(unitid) then
		threat = 3
	end

	if self.db.profile.deficit and m > 0 then
		p = 1 - p
	elseif m < 0 or (not UnitIsConnected(unitid)) then
		p = 1
		color = {r = 0.5, g = 0.5, b = 0.5}
	else
		if self.db.profile.agrocolor and threat > 0 then
			color.r, color.g, color.b = GetThreatStatusColor(threat)
		elseif self.db.profile.classcolor and (frame:GetParent().head == "core") then
			local _, rc = UnitClass(frame.unit)
			if rc then
				color = RAID_CLASS_COLORS[rc]
			else
				color = {r = 0.8, g = 0.8, b = 0.8}
			end
		else
			if p >= 0.5 then
				color = {r = (1 - p) * 2, g = 1, b = 0}
			else
				color = {r = 1, g = p * 2, b = 0}
			end
		end
	end

	if self.db.profile.vertical then
		size = bar:GetHeight()
		bar.front:SetHeight(self:BarSize(size, p))
	else
		size = bar:GetWidth()
		bar.front:SetWidth(self:BarSize(size, p))
	end
BunchOfBars:Print(self:BarSize(size, p))
	bar.front:SetVertexColor(color.r, color.g, color.b)
	bar.back:SetVertexColor(color.r*0.5, color.g*0.5, color.b*0.5)

end

----------------------------------
--      Option Handlers         --
----------------------------------

function plugin:GetSetAgroColor(_, v)
	if type(v) == "nil" then return self.db.profile.agrocolor end

	if self.db.profile.agrocolor ~= v then
		self.db.profile.agrocolor = v

		if self.db.profile.agrocolor then
			agro = self.core:GetModule("Agro").agro
		else
			agro = { }
		end
	end
end


function plugin:GetSetTexture(_, v)
	if type(v) == "nil" then
		for i, k in pairs(tbl) do
			if k == self.db.profile.texture then
				return i
			end
		end
	end

	if self.db.profile.texture ~= tbl[v] then
		self.db.profile.texture = tbl[v]

		self:UpdateAllWith(function(frame)
			frame.parts[moduleName]:SetStatusBarTexture(SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
			frame.parts[moduleName].back:SetTexture(SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
		end)
	end
end


function plugin:GetSetDeficit(_, v)
	if type(v) == "nil" then return self.db.profile.deficit end

	if self.db.profile.deficit ~= v then
		self.db.profile.deficit = v

		self:UpdateAll()
	end
end


function plugin:GetSetClassColor(_, v)
	if type(v) == "nil" then return self.db.profile.classcolor end

	if self.db.profile.classcolor ~= v then
		self.db.profile.classcolor = v

		self:UpdateAll()
	end
end
function plugin:SetTexture()
	self:UpdateAllWith(function(frame)
		frame.parts[moduleName]:SetStatusBarTexture(self.db.profile.texture)
		frame.parts[moduleName].back:SetTexture(self.db.profile.texture)
	end)
end